<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use DB;
use Illuminate\Http\Request;

class HomeController extends Controller
{
    public function index()
    {
//        获取热销推荐的数据
        $hotsale = DB::table('goods')->orderBy('sales', 'desc')->limit(5)->get();
//        获取商品数据
        $goods = DB::table('goods as g1')->get();
        foreach ($goods as $k => $v) {
            $path = explode(',', $v->path);
//         修改返回的数据，以便再前台展示
            $goods[$k]->path = $path[1];
//          同时把path存入数组，验证分区数据中id是否在patharr数组中，方便前台隐藏没有商品的分区
            $patharr[] = $path[1];
        }
        //        获取分区数据
        $plates = DB::table('plate')
            ->where('pid', 0)
            ->get();
        foreach ($plates as $k => $v) {
            if (!in_array("$v->id", $patharr)) {
                unset($plates[$k]);
            }
        }
//        获取商品品牌数据
        $sonPlates = DB::table('plate')
            ->where('pid', '!=', '0')
            ->get();
        $allPlates = DB::table('plate')
            ->where('pid', 0)
            ->get();
        return view('home.index.index')
            ->with([
                'hotsale'   => $hotsale,
                'plates'    => $plates,
                'allPlates' => $allPlates,
                'sonPlates' => $sonPlates,
                'goods'     => $goods]);
    }

    public function search(Request $request)
    {
        $keywords = $request->only('keywords')['keywords'];
        $data = DB::table('goods')->where('goodsname','like','%'.$keywords.'%')->get();
        $sonPlates = DB::table('plate')
            ->where('pid', '!=', '0')
            ->get();
        $allPlates = DB::table('plate')
            ->where('pid', 0)
            ->get();
        return view('/home/plate/search')->with([
            'data'=>$data,
            'allPlates' => $allPlates,
            'sonPlates' => $sonPlates,
            ]);
    }

    public function smallPlate($id)
    {
//        根据id查出pid和自己板块名字
        $res = DB::table('plate as p1')
            ->leftJoin('plate as p2', 'p1.id', '=', 'p2.pid')
            ->where('p2.id', $id)
            ->select('p1.platename as pname', 'p2.platename as platename', 'p1.id as pid', 'p2.id')
            ->first();
        $plates = DB::table('plate')->where('pid', $res->pid)->limit(8)->get();
//        获取该系列下的商品$id为传递的板块的id
        $goods = DB::table('goods')->where('pid', $id)->get();
        $sonPlates = DB::table('plate')
            ->where('pid', '!=', '0')
            ->get();
        $allPlates = DB::table('plate')
            ->where('pid', 0)
            ->get();
        return view('home.plate.plate')->with([
            'res' => $res, 'plates' => $plates, 
            'goods' => $goods,
            'allPlates' => $allPlates,
            'sonPlates' => $sonPlates,
            ]);
    }

    public  function menuplate($id)
    {
        switch ($id) {
            case 1:
                $data = DB::table('goods')->orderBy('addtime','desc')->limit(8)->get();
                $pltname = '新品上市专区';
                break;
            case 2:
                $data = DB::table('goods')->orderBy('price')->limit(8)->get();
                $pltname = '降价特惠专区';
                break;
            case 3:
                $data = DB::table('goods')->orderBy('sales')->limit(8)->get();
                $pltname = '火爆销售专区';
                break;
        }
        $sonPlates = DB::table('plate')
            ->where('pid', '!=', '0')
            ->get();
        $allPlates = DB::table('plate')
            ->where('pid', 0)
            ->get();
        return view('home.plate.menuplate')->with([
            'data'=>$data, 
            'pltname'=>$pltname, 
            'allPlates' => $allPlates,
            'sonPlates' => $sonPlates,
        ]);
    }
}
